package test;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by 下 on 2017/5/3.
 */
public class DBhelper {
    private static final String driver="com.mysql.jdbc.Driver";
    private static final String url="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8";
    private static final String username="root";
    private static final String password="";
    private static Connection conn=null;


    //静态代码块负责加载驱动
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static Connection getConn() throws SQLException {
        if (conn==null){
            conn=DriverManager.getConnection(url,username,password);
        }
        return conn;
    }


    public static void main(String[] args) {
        try {
            Connection conn=DBhelper.getConn();
            if (conn!=null){
                System.out.println("成功");
            }else {
                System.out.println("失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
